<?php
class Banners extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
	}
	
	function index()
	{
		$data['banners'] = new banner;
		if(!empty($_GET['agency_id'])) $data['banners']->where('agency_id', $_GET['agency_id']);
		$data['banners']->order_by('id','desc')->get_page();
		$this->template->append_metadata(js_checkbox('approve'));
		$this->template->build('admin/banner_index',$data);
	}
	
	function form($id=NULL)
	{

		$data['banner'] = new banner($id);
		$this->template->build('admin/banner_form',$data);
	}
	
	function save($id=NULL)
	{
		if($_POST)
		{
			$banner = new banner($id);
			if(@$_FILES['image']['name'])
			{
				if($id)$banner->delete_file('uploads/banner','image');
				$banner->image = $banner->upload($_FILES['image'],'uploads/banner/',185,74);
			}
			$banner->user_id = $this->session->userdata('id');
			$banner->from_array($_POST);
			$banner->save();
			set_notify('success', lang('save_data_complete'));
		}
		redirect('banners/admin/banners');
	}
	
	function approve($id)
	{
		if($_POST)
		{
			$banner = new banner($id);
			$banner->from_array($_POST);
			$banner->save();
		}

	}
	
	function delete($id=FALSE)
	{
		if($id)
		{
			$banner = new banner($id);
			$banner->delete_file('uploads/banner','image');
			$banner->delete();
			set_notify('success', lang('delete_data_complete'));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}

}
?>